﻿#ifndef LOCALTCPCLIENT_H
#define LOCALTCPCLIENT_H

#include "hv/TcpClient.h"
#include "hv/htime.h"
#include "CEvtVar.h"
#include <mutex>

#define SYNC_SEND_DATA_ERR (4)

class localTcpClient
{
public:
    localTcpClient();
    ~localTcpClient();
    bool init(const int port, const std::string host = "127.0.0.1");
    bool connect();
    void disconnect();
    bool isConnected();
    void sendMsg(std::string msg);

    void sendData(char *data, int data_len);
    std::function<int(char*, size_t)> client_cb;

private:
    int m_port;
    std::string m_host;
    hv::TcpClient *myClient;

    int connfd;
    bool is_connect = false;
    reconn_setting_t m_reconn;
};


class localTcpClientSync:public localTcpClient
{
public:
    localTcpClientSync();

    int sync_recv_data(char *buf, size_t sz);
    int sync_send_data(char *buf, size_t sz,int nms);

    char mData[1024]={};                       //只保留最后一次接收的数据，需要保证回复的数据是一次传完的
    int mDataLen=0;
private:
    CEvtVar evtVar;
};

#endif // LOCALTCPCLIENT_H
